package com.hospital.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.hospital.entity.Employee;
import com.hospital.mapper.EmployeeMapper;
import com.hospital.tools.JsonResult;
import com.hospital.tools.MD5Utils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    EmployeeMapper employeeMapper;

    // 获取所有员工数据
    @GetMapping("/get_all")
    public JsonResult getAllEmployees() {
        return JsonResult.json_ok(employeeMapper.getAllEmployee());
    }

    // 通过 Eid 获取用户数据
    @PostMapping("/get_by_id")
    public JsonResult getEmployeeById(
            @Valid @NotNull(message = "员工ID不得为空") @RequestBody String params,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(params);
            Integer Eid = jsonObject.getInteger("eid");
            if (Eid == null) {
                return JsonResult.json_error("100", "eid不可为空");
            }
            Employee e = employeeMapper.getEmployeeByID(Eid);
            if (e == null) {
                return JsonResult.json_error("303", "该员工不存在");
            }
            return JsonResult.json_ok(e);
        } catch (NumberFormatException e) {
            return JsonResult.json_error("304", "参数格式错误");
        }

    }

    @PostMapping("/get_by_phone")
    public JsonResult getEmployeeByPhone(
            @Valid @NotNull(message = "员工ID不得为空") @RequestBody String params,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }

        JSONObject jsonObject = JSONObject.parseObject(params);

        String phone = jsonObject.getString("phone");
        if (phone == null || "".equals(phone)) {
            return JsonResult.json_error("100", "手机号不可为空");
        }
        Employee employee = employeeMapper.getEmployeeByPhone(phone);
        if (employee == null) {
            return JsonResult.json_error("303", "该员工不存在");
        }
        return JsonResult.json_ok(employee);
    }

    // 登录
    @PostMapping("/login")
    public JsonResult login(
            @Valid @NotNull(message = "参数不可为空") @RequestBody String params,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }

        JSONObject jsonObject = JSONObject.parseObject(params);

        String Phone = jsonObject.getString("phone");
        if (Phone == null || "".equals(Phone)) {
            return JsonResult.json_error("301", "手机号不得为空");
        }

        String Password = jsonObject.getString("password");
        if (Password == null || "".equals(Password)) {
            return JsonResult.json_error("302", "密码不得为空");
        }
        String TruePassword = employeeMapper.getEmployeePasswordByPhone(Phone);

        if (TruePassword == null) {
            return JsonResult.json_error("303", "账户不存在");
        }

        if (TruePassword.equals(MD5Utils.getMD5(Password))) {
            return JsonResult.json_ok(employeeMapper.getEmployeeByPhone(Phone));
        }
        return JsonResult.json_error("304", "密码错误");
    }

    // 添加医院员工接口
    @PostMapping("/add")
    public JsonResult addEmployee(
            @Valid @RequestBody Employee employee,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }

        try {
            int res = employeeMapper.insertEmployee(employee);
            return res == 1 ?
                    JsonResult.json_ok("添加成功") :
                    JsonResult.json_error("401", "添加失败，原因未知");
        } catch (DuplicateKeyException e) {
            return JsonResult.json_error("400", "手机号码已存在");
        }

    }
    // 医院员工接口
    @PostMapping("/update")
    public JsonResult updateEmploy(
            @Valid @RequestBody Employee employee,
            BindingResult bindingResult
    ) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            // 100 为参数错误
            return JsonResult.json_error("100", error.getDefaultMessage());
        }

        try {
            int res = employeeMapper.updateEmployee(employee);
            return res == 1 ?
                    JsonResult.json_ok("修改成功") :
                    JsonResult.json_error("401", "修改失败，原因未知");
        } catch (DuplicateKeyException e) {
            return JsonResult.json_error("400", "手机号码冲突");
        } catch (Exception e) {
            return JsonResult.json_error("400", e.getMessage());
        }
    }
}


